<?php

namespace App\Observers;

use App\Models\ForumTopic;
use App\Jobs\TranslateSlug;
// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class ForumTopicObserver
{
    public function creating(ForumTopic $forum_topic)
    {
        //
    }

    public function updating(ForumTopic $forum_topic)
    {
        //
    }
    public function saving(ForumTopic $topic)
    {
        $topic->body = clean($topic->body, 'user_topic_body');
        $topic->excerpt = make_excerpt($topic->body);
    }
    public function saved(ForumTopic $topic)
    {

        // 如 slug 字段无内容，即使用翻译器对 title 进行翻译
        if (!$topic->slug) {
            //$slug = app(\App\Handlers\SlugTranslateHandler::class)->translate("黄金多少钱？");
            //dd($slug);
            // 推送任务到队列
            dispatch(new TranslateSlug($topic));
        }
    }
    public function deleted(Topic $topic)
    {
        \DB::table('replies')->where('topic_id', $topic->id)->delete();
    }
}